var hdcurrentstate = (function (){
	
	this.init = function (){
		$("#btnAddSumView").click(function(){
            hdadd.updateAdd();
            $.mobile.changePage("#page2");
        });
        $("#btnSpendSum").click(function(){
            hdtake.updateTake();
            $.mobile.changePage("#page3");
        });
        $("#btnCurrentStateCancel").click(function (){
        	hdmain.updateData();
        });
        $("#btnSavingSum").click(function(){
            hdsavesum.updateSaveSum();
            $.mobile.changePage("#pgSaveSum");
        });
        $("#btnReservingSum").click(function(){
            hdreservsum.updateReserveSum();
            $.mobile.changePage("#pgReserveSum");
        });
        $("#btnTransferSum").click(function(){
        	hdtransfersum.updateTransferSum();
        	$.mobile.changePage("#pgTransferSum");
        });
	};
    this.show = function (){
        this.updatecurrentstate();
        $.mobile.changePage("#CurrentState");
    };
	this.updatecurrentstate = function (){
		var currentSum = 0, daysLeft = 0, reservedSum = 0;
		data.getReservedSum( function (sum){
			reservedSum = sum;
		});
		data.getSumState(function (results){
            var tempSD, tempED, sd, ed, tempSum, updatedDates;
            updatedDates = UpdateDates(results.item(0).StartDate, results.item(0).EndDate)
            daysLeft = updatedDates.daysBetween;
            currentSum = results.item(0).Sum - reservedSum;
            $("#spCurrentSumState").html(addCommas(currentSum) + " RSD");
        });
        data.getTodayAddedData(function (results){
        	if(results.item(0).Sum !== null){
        		$("#spAddedToday").html(addCommas(results.item(0).Sum) + " RSD");
        	}else{
        		$("#spAddedToday").html("0.00 RSD");
        	}
        });
        data.getTodayTakenData(function (results){
        	if(results.item(0).Sum !== null){
        		var oldState = 0, dailySum = 0,tempCurrentSum;
        		$("#spTakenToday").html(addCommas(results.item(0).Sum) + " RSD");
        		oldState = parseInt((results.item(0).Sum*100)+(currentSum*100))/100;
                dailySum = parseInt((oldState/daysLeft)*100)/100;
                tempCurrentSum = currentSum;
                $("#spCurrentTodayState").html(addCommas( parseInt((dailySum*100)-(results.item(0).Sum*100))/100) + " RSD");
                if(results.item(0).Sum > dailySum){
                    tempCurrentSum = roudNumber(oldState-(results.item(0).Sum - dailySum))
                    //$("#spCurrentSumState").html(addCommas(currentSum) + " RSD");
                    dailySum = parseInt((tempCurrentSum/daysLeft)*100)/100;
                }
            	$("#spDailySumState").html(addCommas( dailySum) + " RSD");
            	
        	}else{
        		$("#spTakenToday").html("0.00 RSD");
            	$("#spDailySumState").html(addCommas( roudNumber(((parseInt((currentSum*100))/100)/daysLeft))) + " RSD");
            	$("#spCurrentTodayState").html(addCommas(roudNumber((((currentSum*100))/100)/daysLeft)) + " RSD");
        	}
        });
        data.getTodaySavedData(function (data){
        	if(data.item(0).Sum !== null){
        		var oldState = 0, dailySum = 0;
        		$("#spSavedSumToday").html(addCommas(data.item(0).Sum) + " RSD");
        	}else{
        		$("#spSavedSumToday").html("0.00 RSD");
        	}
        });
        data.getTodayReservedData(function (data){
        	if(data.item(0).Sum !== null){
        		var oldState = 0, dailySum = 0;
        		$("#spReservedSumToday").html(addCommas(data.item(0).Sum) + " RSD");
        	}else{
        		$("#spReservedSumToday").html("0.00 RSD");
        	}
        });

	};
	this.init();
	return this;
}(hdcurrentstate || {}))
